/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hellicopter_tester;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.*;

/**
 *
 * @author Törcsi
 */
public class UDP {
    
    
    public static String fromHexString(String hex) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < hex.length(); i+=2) {
            str.append((char) Integer.parseInt(hex.substring(i, i + 2), 16));
        }
        return str.toString();
    }
    public static String toHexString(byte[] ba) {
        StringBuilder str = new StringBuilder();
        for(int i = 0; i < ba.length; i++)
            str.append(String.format("%x", ba[i]));
        return str.toString();
    }
    


    public static void start(int iport,String ip, boolean response, byte[] sentence) throws Exception
   {
      BufferedReader inFromUser =
         new BufferedReader(new InputStreamReader(System.in));
      DatagramSocket clientSocket = new DatagramSocket();
      InetAddress IPAddress = InetAddress.getByName(ip);
      byte[] sendData = new byte[4];
      byte[] receiveData = new byte[1024];
      //String sentence = inFromUser.readLine();
      //sentence=fromHexString(sentence);
      //System.out.println(sentence);
      //System.out.println(toHexString(sentence.getBytes()));
      //sendData = sentence.getBytes();
      sendData=sentence;
      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, iport);
      clientSocket.send(sendPacket);
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      if(response)
      {
      clientSocket.receive(receivePacket);
      String modifiedSentence = new String(receivePacket.getData());
      System.out.println("FROM SERVER:" + modifiedSentence);
      }
      clientSocket.close();
   }
}
